<script setup lang="ts">
import { reactive, ref, onMounted } from "vue";
import { useMenu } from "./utils/hook";
import { transformI18n } from "@/plugins/i18n";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import TreeList from "@/components/TreeList.vue";
import Delete from "@iconify-icons/ep/delete";
import EditPen from "@iconify-icons/ep/edit-pen";
import Refresh from "@iconify-icons/ep/refresh";
import AddFill from "@iconify-icons/ri/add-circle-line";

defineOptions({
  name: "powerList"
});

const formRef = ref();
const tableRef = ref();
let {
  submitSon,
  selectHandle,
  closeDialog,
  treeInfoRef,
  formVisible,
  formChild,
  cropper,
  changesystem,
  pagination,
  selectPag,
  dataChange,
  roleOptions,
  roleOptions2,
  onChange,
  form,
  loading,
  columns,
  dataList,
  onSearch,
  resetForm,
  openDialog,
  handleDelete,
  handleSelectionChange,
  handleSizeChange,
  handleCurrentChange
} = useMenu();

function onFullscreen() {
  // 重置表格高度
  tableRef.value.setAdaptive();
}
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px] overflow-auto"
    >
      <el-form-item label="应用系统：" prop="systemCode">
        <el-select
          v-model="selectPag.systemCode"
          placeholder="请选择"
          clearable
          class="!w-[180px]"
          @change="changesystem"
        >
          <el-option
            v-for="(item, index) in roleOptions"
            :key="index"
            :value="item.applicationCode"
            :label="item.applicationName"
          >
            {{ item.applicationName }}
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="权限名称：" prop="taskCode">
        <el-select
          v-model="selectPag.taskCode"
          placeholder="请选择"
          clearable
          class="!w-[180px]"
        >
          <el-option
            v-for="(item, index) in roleOptions2"
            :key="index"
            :value="item.taskCode"
            :label="item.taskCname"
          >
            {{ item.taskCname }}
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否有效：" prop="validStatus">
        <el-select
          v-model="selectPag.validStatus"
          placeholder="请选择"
          clearable
          class="!w-[180px]"
        >
          <el-option label="有效" value="1" />
          <el-option label="无效" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间：" prop="value1">
        <el-date-picker
          v-model="selectPag.value1"
          type="datetimerange"
          range-separator="到"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          format="YYYY-MM-DD HH:mm:ss"
          value-format="YYYY-MM-DD HH:mm:ss"
          @change="dataChange"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon('ri:search-line')"
          :loading="loading"
          @click="onSearch"
        >
          搜索
        </el-button>
        <!-- <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          重置
        </el-button> -->
      </el-form-item>
    </el-form>

    <PureTableBar
      title="功能权限列表"
      :columns="columns"
      :isExpandAll="false"
      :tableRef="tableRef?.getTableRef()"
      @refresh="onSearch"
      @fullscreen="onFullscreen"
    >
      <template #buttons>
        <el-button
          type="primary"
          :icon="useRenderIcon(AddFill)"
          @click="openDialog('新增')"
        >
          新增权限
        </el-button>
      </template>
      <el-table
        :data="dataList"
        style="width: 100%"
        stripe
        row-key="id"
        :tree-props="{ children: 'childSysTasks' }"
        border
        :header-cell-style="{
          background: 'var(--el-fill-color-light)',
          color: 'var(--el-text-color-primary)'
        }"
      >
        <el-table-column label="权限名称" prop="taskCname" />
        <el-table-column label="应用系统" prop="systemName" />
        <el-table-column label="权限描述" prop="remark" />
        <el-table-column label="状态" prop="enabled">
          <template #default="{ row }">
            <el-switch
              v-model="row.validStatus"
              class="ml-2"
              active-value="{1}"
              inactive-value="{0}"
              active-text="已启用"
              inactive-text="已停用"
              inline-prompt
              @change="onChange"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" prop="name">
          <template #default="{ row }">
            <el-button
              class="reset-margin"
              link
              type="primary"
              :icon="useRenderIcon(EditPen)"
              @click="openDialog('修改', row)"
            >
              修改
            </el-button>
            <el-popconfirm
              :title="`是否确认删除权限名称为${row.taskCname}的这条数据。注意如果有下级权限，也会一并删除，请谨慎操作！`"
              @confirm="handleDelete(row)"
            >
              <template #reference>
                <el-button
                  class="reset-margin"
                  link
                  type="primary"
                  :icon="useRenderIcon(Delete)"
                >
                  删除
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin: 20px 0; display: flex; justify-content: end">
        <el-pagination
          v-model:current-page="selectPag.pageNum"
          v-model:page-size="selectPag.pageSize"
          :page-sizes="[5, 10, 30, 100]"
          size="default"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="selectPag.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </PureTableBar>
    <el-dialog
      v-model="formVisible"
      title="编辑"
      :width="680"
      draggable
      :before-close="closeDialog"
    >
      <div>
        <el-form
          ref="formChildRef"
          :inline="false"
          :model="formChild"
          :label-width="200"
        >
          <el-form-item label="权限代码：" prop="taskCode">
            <el-input
              v-model="formChild.taskCode"
              placeholder="请输入权限代码"
              clearable
              class="!w-[360px]"
            />
          </el-form-item>
          <el-form-item label="权限名称：" prop="taskCname">
            <el-input
              v-model="formChild.taskCname"
              placeholder="请输入权限名称"
              clearable
              class="!w-[360px]"
            />
          </el-form-item>
          <el-form-item label="应用系统：" prop="systemName">
            <el-input
              v-model="formChild.systemName"
              disabled
              clearable
              class="!w-[360px]"
            />
          </el-form-item>
          <el-form-item label="选择菜单：" prop="menuName">
            <el-input
              v-model="formChild.menuName"
              placeholder="请选择选择菜单"
              clearable
              disabled
              class="!w-[360px]"
            />
            <el-button type="primary" @click="selectHandle">选择</el-button>
          </el-form-item>
          <el-form-item label="权限类型：" prop="powerType">
            <el-select
              v-model="formChild.powerType"
              placeholder="请选择"
              clearable
              disabled
              class="!w-[360px]"
            >
              <el-option
                v-for="(item, index) in roleOptions2"
                :key="index"
                :value="item.value"
                :label="item.label"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="权限描述：" prop="remark">
            <el-input
              type="textarea"
              v-model="formChild.remark"
              placeholder="权限描述"
              clearable
              class="!w-[360px]"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :loading="loading" @click="submitSon">
              提交
            </el-button>
            <el-button @click="closeDialog()"> 取消 </el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
    <TreeList ref="treeInfoRef" @cropper="cropper"></TreeList>
  </div>
</template>

<style lang="scss" scoped>
:deep(.el-table__inner-wrapper::before) {
  height: 0;
}

.main-content {
  margin: 24px 24px 0 !important;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
